﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.IO;
using System.IO.IsolatedStorage;

namespace BapVeBienCuong.menu
{
    public class DiemCao
    {
        public static string[] ten = new string[12];
        public static int[] diem = new int[12];
        public string temp;
        static IsolatedStorageFile storage;
        public DiemCao()
        {
            for (int k = 0; k < 12; k++)
            {
                diem[k] = 0;
                ten[k] = "";
            }
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!storage.FileExists("sunnet_l12sq_DiemCao.txt"))
            {
                using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("sunnet_l12sq_DiemCao.txt", FileMode.Create, FileAccess.Write, storage)))
                {
                    writer.WriteLine("Cổ Loa Thành;0");
                    writer.WriteLine("Sông Hồng;0");
                    writer.WriteLine("Thành Hoa Lư;0");
                    writer.WriteLine("Tam Điệp;0");
                    writer.WriteLine("Điện Thái Hòa;0");
                    writer.WriteLine("Đường Lâm;0");
                    writer.WriteLine("Phong Châu;0");
                    writer.WriteLine("Tiêu Du;0");
                    writer.WriteLine("Bố Hải Khẩu;0");
                    writer.WriteLine("Chi Lăng;0");
                    writer.WriteLine("Đỗ Động Giang;0");
                    writer.WriteLine("Thăng Long Thành;0");
                    writer.Flush();
                    writer.Close();
                    writer.Dispose();
                }
            }
            IsolatedStorageFileStream f = storage.OpenFile("sunnet_l12sq_DiemCao.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
            int i = 0;
            int daungancach;
            using (StreamReader r = new StreamReader(f))
            {
                do
                {
                    temp = r.ReadLine();
                    daungancach = temp.LastIndexOf(";");
                    ten[i] = temp.Substring(0, daungancach);
                    diem[i] = Convert.ToInt32(temp.Substring(daungancach + 1, temp.Length - daungancach - 1));
                    i++;
                } while (!r.EndOfStream);
                r.Close();
                r.Dispose();
            }//mở file ra để đọc
        }
        public bool isHigh(int level, int ndiem)
        {
            if (diem[level] < ndiem)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void themdiem(int level, int ndiem)
        {
            diem[level] = ndiem;
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (storage.FileExists("sunnet_l12sq_DiemCao.txt"))
            {
                storage.DeleteFile("sunnet_l12sq_DiemCao.txt");
            }
            using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("sunnet_l12sq_DiemCao.txt", FileMode.Create, FileAccess.Write, storage)))
            {
                for (int t = 0; t < 12; t++)
                {
                    writer.WriteLine(ten[t] + ";" + diem[t].ToString());
                }
                writer.Flush();
                writer.Close();
                writer.Dispose();
            }
            storage.Dispose();
        }
        public void Draw(SpriteBatch sprite, SpriteFont font)
        {
            int temp = 0;
            for (int i = 0; i < 12; i++)
            {
                if (diem[i] != 0)
                {
                    sprite.DrawString(font, ten[i], new Vector2(200, 85 + 25 * i), Color.White);
                    sprite.DrawString(font, diem[i].ToString(), new Vector2(500, 85 + 25 * i), Color.White);
                }
                else temp++;
            }
            if (temp == 12) sprite.DrawString(font, "Chưa có thành tích", new Vector2(300, 200), Color.White);
        }
        public int getDiemCao(int lever)
        {
            return diem[lever];
        }
    }
}
